﻿package  
{
	import as3.collections.IMap;
	import as3.collections.LinkedMap;
	import commands.ProductListCommand;
	/**
	 * ...
	 * @author Rami
	 */
	public class Reactor
	{
		private var commandsMap:IMap;
		public function Reactor() 
		{
			this.commandsMap = new LinkedMap();
			this.registerCommand(ProductListCommand.NAME , new ProductListCommand());
		}
		public function registerCommand(commandName:String, commandObj : ICommand):void
		{
			this.commandsMap.addItem(commandName , commandObj);
		}
		
		public function executeCommands(commandsBody:String):void
		{
			var xmlData:XML = new XML(commandsBody);
			
			for each (var command:XML in xmlData.command)
			{
				trace("Command Name : " + command.@name);
				this.executeCommand(command);
			}
		}
		private function executeCommand(commandBody:XML):void
		{
			var commandName:String = this.retriveCommandName(commandBody);
			var  command : ICommand = this.commandsMap.getItem(commandName) as ICommand;
			if (command != null)
			{
				command.execute(commandBody);
			}
		}

		private function retriveCommandName(commandBody:XML):String
		{
			var commandName: String =  (commandBody.@name).toString();
			return commandName;
		}
		
	}

}